<?php
namespace Home\Controller;

use Think\Controller;

/**
*@desc 分类列表
*/
class CategoryController extends CommonController {
	private $pagesize = 20;
	
    public function __construct(){
        parent::__construct();

        // 用户信息
        if($this->role != -1){// 游客
            $this->user_email = session('user_email');
            $this->uid = session('uid');
            $this->assign('user_email',$this->user_email);
            $this->user_token = session('user_token');
        }

        $this->assign('role',$this->role);
    }

    public function index(){
        //一级分类编号
    	$cid = I('get.cid',0,'intval');
        $second_cid = I('get.tid',0,'intval');//二级分类编号

//      //分类信息
//      $categoryModel = D('Category');
//
//      $all_category_list = $categoryModel->allCategoryList();
//      //一级二级分类递归处理
//      $all_category_array = recursionWithCategoroy($all_category_list);
//      $this->assign('category_list',$all_category_array );
//      $all_json_category_string = json_encode($all_category_array);
//      $this->assign('json_category_string',$all_json_category_string);

        $this->assign('cid',$cid);
        $this->display();
    }

    //精品/热门页面
    public function recommend(){
    	$type = I('get.type','recommend');

        //分类信息
        $categoryModel = D('Category');
        $category_list = $categoryModel->getAllParentCategoryList();
        $this->assign('category_list',$category_list);

        $this->assign('type',$type);
        $this->display();
    }

    //分类ajax数据加载
    public function ajaxCategoryList(){
    	$cid = I('post.cid',0,'intval');
        $tid = I('post.tid',0,'intval');
    	$page = I('post.page',1,'intval');
    	//当前排序规则
    	$order = I('post.order',1,'intval');

    	// if(empty($cid) || !is_numeric($cid)) $this->ajaxReturn( array('status'=>1003,'msg'=>'缺少必要参数','data'=>null) );

    	//服务列表
        $serviceModel = D('Services');
        $list = $serviceModel->getListWithCategory($cid,$tid,$order,$page,$this->pagesize);

        $data['page'] = $page;
        $data['total_num'] = $list['total_num'];
        $data['totalpage'] = ceil($list['total_num']/$this->pagesize);
        $data['list'] = $list['list'];
        $data['order'] = $order;

        $this->ajaxReturn( array('status'=>200,'msg'=>'Ok','data'=>$data) );
    }

    //精品或热门数据ajax加载
    public function ajaxRecommentList(){
    	$data_type = I('post.data_type','recommend');
    	$page = I('post.page',1,'intval');
    	//当前排序规则
    	$order = I('post.order',1,'intval');

    	if($data_type=="hot"){
    		$service_type_id = $this->recommend_id;
    	}else{
    		$service_type_id = $this->boutique_id;
    	}

    	$serviceModel = D('ServiceRecommend');
    	$list = $serviceModel->getRecommendListWithOrder($service_type_id,$order,$page,$this->pagesize);

    	$data['page'] = $page;
        $data['total_num'] = $list['total_num'];
        $data['totalpage'] = ceil($list['total_num']/$this->pagesize);
        $data['list'] = $list['list'];
        $data['order'] = $order;

        $this->ajaxReturn( array('status'=>200,'msg'=>'Ok','data'=>$data) );

    }

}
